﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace laohan.Common.Common
{
    public partial class API
    {
        /// <summary>
        /// 字节转图片
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static Bitmap BytesToBitmap(byte[] bytes)
        {
            try
            {
                MemoryStream ms = new MemoryStream(bytes);

                Bitmap bm = (Bitmap)Image.FromStream(ms);

                ms.Close();

                return bm;
            }
            catch (Exception ex)
            {
                throw new Exception("字节数组转图片发生错误，错误原因：" + ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="byteArrayIn"></param>
        /// <returns></returns>
        public static Image ByteArrayToImage(byte[] bytes)
        {
            if (bytes == null)
            {
                return null;
            }

            using (var ms = new MemoryStream(bytes))
            {
                var returnImage = Image.FromStream(ms);

                ms.Flush();
                
                return returnImage;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bm"></param>
        /// <returns></returns>
        public static byte[] BitmapToBytes(Bitmap bm)
        {
            try
            {
                MemoryStream ms = new MemoryStream();

                bm.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

                byte[] bytes = ms.GetBuffer();

                ms.Close();

                return bytes;
            }
            catch (Exception ex)
            {
                throw new Exception("图片转字节数组发生错误，错误原因：" + ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bm"></param>
        /// <returns></returns>
        public static string BitmapToBase64(Bitmap bm)
        {
            var bytes = BitmapToBytes(bm);

            return BytesToBase64String(bytes);
        }

        /// <summary>
        /// ICON转bitmap
        /// </summary>
        /// <param name="icon"></param>
        /// <returns></returns>
        public static Bitmap IconToBitmap(Icon icon)
        {
            try
            {
                MemoryStream ms = new MemoryStream();

                icon.Save(ms);

                Bitmap bm = (Bitmap)Image.FromStream(ms);

                ms.Close();

                return bm;
            }
            catch (Exception ex)
            {
                throw new Exception("字节数组转图片发生错误，错误原因：" + ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static byte[] ImageToBytes(Image image)
        {
            if (image == null)
            {
                return null;
            }

            var format = image.RawFormat;

            using (MemoryStream ms = new MemoryStream())
            {
                if (format.Equals(ImageFormat.Jpeg))
                {
                    image.Save(ms, ImageFormat.Jpeg);
                }
                else if (format.Equals(ImageFormat.Png))
                {
                    image.Save(ms, ImageFormat.Png);
                }
                else if (format.Equals(ImageFormat.Bmp))
                {
                    image.Save(ms, ImageFormat.Bmp);
                }
                else if (format.Equals(ImageFormat.Gif))
                {
                    image.Save(ms, ImageFormat.Gif);
                }
                else if (format.Equals(ImageFormat.Icon))
                {
                    image.Save(ms, ImageFormat.Icon);
                }
                else
                {
                    image.Save(ms, ImageFormat.Jpeg);
                }

                byte[] buffer = new byte[ms.Length];

                ms.Seek(0, SeekOrigin.Begin);//Image.Save()会改变MemoryStream的Position，需要重新Seek到Begin
                ms.Read(buffer, 0, buffer.Length);

                return buffer;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="image"></param>
        public static string ImageToBase64(Image image)
        {
            var bytes = ImageToBytes(image);

            if (bytes == null)
            {
                return string.Empty;
            }

            return BytesToBase64String(bytes);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ctr"></param>
        /// <returns></returns>
        public static byte[] CtrToImage(Control ctr)
        {
            Point screenPoint = ctr.PointToScreen(new Point());

            Rectangle rect = new Rectangle(screenPoint, ctr.Size);

            Image img = new Bitmap(rect.Width, rect.Height);
            Graphics g = Graphics.FromImage(img);

            g.CopyFromScreen(rect.X - 1, rect.Y - 1, 0, 0, rect.Size);

            return ImageToBytes(img);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ctr"></param>
        /// <returns></returns>
        public static string CtrToImageBase64(Control ctr)
        {
            Point screenPoint = ctr.PointToScreen(new Point());

            Rectangle rect = new Rectangle(screenPoint, ctr.Size);

            Image img = new Bitmap(rect.Width, rect.Height);
            Graphics g = Graphics.FromImage(img);

            g.CopyFromScreen(rect.X - 1, rect.Y - 1, 0, 0, rect.Size);

            return ImageToBase64(img);
        }
    }
}